package com.fingard.dsp.bank.directbank.cmb01;

import com.fingard.dsp.bank.directbank.bankInterface.IMailedMsg;
import com.fingard.dsp.bank.reqrespobj.mailedmsg.ReqMailedMsgQry;
import com.fingard.dsp.bank.reqrespobj.mailedmsg.RetAtsMailedMsg;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * Created by F on 2019/9/21.
 */
public class CMB01MailedMsg extends CMB01Base implements IMailedMsg {
    @Override
    public RetAtsMailedMsg getMailedMsg(ReqMailedMsgQry qry) throws Exception {
        RetAtsMailedMsg retAts = new RetAtsMailedMsg(qry.reqHeader);
        retAts.writeStart();
        try {
            CMB01Header header =  new CMB01Header();
            header.FUNNAM = "TDAFSDCR";
            XmlTextWriter xmlWriter =  new XmlTextWriter();
            writeCommonHeader(xmlWriter,header);
//            输入	TDAFSDCRX	输入属性	单记录
            xmlWriter.writeStartElement("TDAFSDCRX");
//            直连对接企业系统客户号	COREID	C(10)	否	长安主机厂对应的10位客户号
            xmlWriter.writeElementString("COREID",bankFront.org );
//            业务参考号	YURREF	C(30)	否
            xmlWriter.writeElementString("YURREF", header.FUNNAM+getYURREF(reqHeader.transSeqID));
//            经销商代码	DLENUM	C(40)	否	经销商的唯一标识
            xmlWriter.writeElementString("DLENUM", qry.DealerCode);
//            订单明细编号	ORDEID	Z(40)	否	订单明细编号，订单信息接收接口的ERP订单号
            xmlWriter.writeElementString("ORDEID", qry.OrderCode);
//            保留字段	REV50L	C(50)	可
            xmlWriter.writeElementString("REV50L","" );
            xmlWriter.writeEndElement();
            while (qry.readRecord()) {
//            输入	TDAFSDCRY	输入属性	多记录
                xmlWriter.writeStartElement("TDAFSDCRY");
//            合同单价	CRRAMT	M	否	提车单价
                xmlWriter.writeElementString("CRRAMT",qry.ReqDetail.PickupPrice );
//            发车日期	SHIDAT	C(20)	否	车辆发运时间,精确到时分秒 格式：YYYY-MM-DD HH:MM:SS (中间空格分隔)
                xmlWriter.writeElementString("SHIDAT",qry.ReqDetail.deliveryTime );
//            车型	CARTYP	Z(40)	可	车型编码
                xmlWriter.writeElementString("CARTYP", qry.ReqDetail.motorcycleType);
//            合格证号	CARNUM	C(40)	否
                xmlWriter.writeElementString("CARNUM", qry.ReqDetail.CertificationNo);
//            发动机号	ENGNUM	C(40)	可
                xmlWriter.writeElementString("ENGNUM", qry.ReqDetail.EngineNo);
//            底盘号/车架号	CHANUM	C(40)	否
                xmlWriter.writeElementString("CHANUM",qry.ReqDetail.VIN );
//            汇票号	BILLNO	C(100)	可	多张汇票用英文逗号隔开，最多2张
                xmlWriter.writeElementString("BILLNO", "");
//            备注	REMARK	Z(256)	可
                xmlWriter.writeElementString("REMARK", "");
                xmlWriter.writeEndElement();
            }
            xmlWriter.writeEndDocument();
            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());

            if(tmpStrRet[0].length() == 0) {
                StringBuilder tmpbkMsg = new StringBuilder();
                //解析银行返回
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpbkMsg);
                if (tmpBkReader != null) {
                    String bankRetCode = tmpBkReader.getFirstText("/CMBSDKPGK/INFO/RETCOD");
                    if("0".equals(bankRetCode)) {
//                        TDAFORRVZ
//                        返回代码	RTNCOD	C(7)	否	返回值：SCDS000则代表成功 其他值则代表失败
//                        返回信息	RTNMSG	Z(256)	可
                        String rtnCod = tmpBkReader.getFirstText("/CMBSDKPGK/TDAFSDCRZ/RTNCOD");
                        String rtnMsg = tmpBkReader.getFirstText("/CMBSDKPGK/TDAFSDCRZ/RTNMSG");
                        if(StringHelper.hasAnyChar(rtnCod)) {
                            if("SCDS000".equals(rtnCod)) {
                                retAts.respCode = "0";
                                retAts.respInfo = rtnMsg;
                            }else {
                                throw new Exception(rtnCod+":"+rtnMsg);
                            }
                        }else{
                            StringBuilder errMsg = new StringBuilder();
                            while (tmpBkReader.read("/CMBSDKPGK/TDAFSDCRW")){
                                errMsg.append(tmpBkReader.readedXElement.getSingleText("ERRMSG")).append("\r\n");
                            }
                            throw new Exception(errMsg.toString().trim());
                        }
                    }else{
                        String bankRetMsg =tmpBkReader.getFirstText("/CMBSDKPGK/INFO/ERRMSG");
                        throw new Exception(bankRetCode+":"+bankRetMsg);
                    }
                }else {
                    throw new Exception(tmpBkReader.toString());
                }
            } else{
                throw new Exception(tmpStrRet[0]);
            }
        }catch (Exception ex){
            ex.printStackTrace();
            retAts.respCode ="-1";
            retAts.respInfo = ex.getMessage();
        }
        retAts.writeRespCode(retAts.respCode);
        retAts.writeRespInfo(retAts.respInfo);
        retAts.writeEnd();
        return retAts;
    }
}
